﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;

namespace LineWatch_WinForm
{
    public partial class ScreenSaver : Form
    {
        private int currentImageIndex = 0;

        public ScreenSaver()
        {
            InitializeComponent();
            this.timer.Enabled = false;
            
#if DEBUG
            this.TopMost = false;
#else
            this.TopMost = true;
#endif
        }

        /// <summary>
        /// Get Pictures
        /// </summary>
        private void GetPictures()
        {
            this.imageList.Images.Clear();

            string pictureName1Line1 = string.Empty;
            string pictureName2Line1 = string.Empty;
            string pictureName3Line1 = string.Empty;
            string pictureName4Line1 = string.Empty;

            string pictureName1Line2 = string.Empty;
            string pictureName2Line2 = string.Empty;
            string pictureName3Line2 = string.Empty;
            string pictureName4Line2 = string.Empty;

            string pictureName1Line3 = string.Empty;
            string pictureName2Line3 = string.Empty;
            string pictureName3Line3 = string.Empty;
            string pictureName4Line3 = string.Empty;

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                pictureName1Line1 = ConfigInfo.GetScreenPictureName1(ConfigInfo.DtLineWatchSys1);
                pictureName2Line1 = ConfigInfo.GetScreenPictureName2(ConfigInfo.DtLineWatchSys1);
                pictureName3Line1 = ConfigInfo.GetScreenPictureName3(ConfigInfo.DtLineWatchSys1);
                pictureName4Line1 = ConfigInfo.GetScreenPictureName4(ConfigInfo.DtLineWatchSys1);

                this.InitImageList(ConfigInfo.LineCode1, pictureName1Line1);
                this.InitImageList(ConfigInfo.LineCode1, pictureName2Line1);
                this.InitImageList(ConfigInfo.LineCode1, pictureName3Line1);
                this.InitImageList(ConfigInfo.LineCode1, pictureName4Line1);
            }
        }

        /// <summary>
        /// Init Image List
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="fileName"></param>
        private void InitImageList(string lineCode, string fileName)
        {
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["UploadFileDirectory"]
                                + "/" + lineCode + "/"
                                + fileName;

                FileInfo file = new FileInfo(filePath);
                if (file.Exists)
                {
                    Image image = Image.FromFile(file.FullName);
                    this.imageList.Images.Add(image);
                }
            }
        }

        /// <summary>
        /// Show Picture
        /// </summary>
        private void ShowPicture()
        {
            int timeSpan = ConfigInfo.GetScreenTimeSpan2(ConfigInfo.DtLineWatchSys1);

            if (timeSpan <= 0)
            {
                return;
            }

            TimeSpan span = DateTime.Now - Util.ScreenSaverRefreshTime;

            if (span.TotalSeconds >= timeSpan)
            {
                if (this.currentImageIndex < this.imageList.Images.Count - 1)
                {
                    this.currentImageIndex++;
                }
                else
                {
                    this.currentImageIndex = 0;
                }

                if (this.imageList.Images.Count > 0)
                {
                    this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];
                    Util.ScreenSaverRefreshTime = DateTime.Now;
                }
            }

            this.pictureBox.Update();
            this.pictureBox.Refresh();

            this.Update();
            this.Refresh();
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void pictureBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_KeyDown(object sender, KeyEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void ScreenSaver_MouseDown(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_FormClosing(object sender, FormClosingEventArgs e)
        {
            Util.PositionNormalTime = DateTime.Now;
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_Load(object sender, EventArgs e)
        {
            this.GetPictures();

            this.currentImageIndex = 0;

            if (this.imageList.Images.Count > 0)
            {
                this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];
            }
        }

        /// <summary>
        /// Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenSaver_Shown(object sender, EventArgs e)
        {
            this.currentImageIndex = 0;

            if (this.imageList.Images.Count > 0)
            {
                this.pictureBox.Image = this.imageList.Images[this.currentImageIndex];
            }

            Util.ScreenSaverRefreshTime = DateTime.Now;

            this.timer.Enabled = true;
        }

        /// <summary>
        /// timer Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.ShowPicture();
        }

    }
}
